package com.study.dubbo.consumer.service.impl;

import com.alibaba.dubbo.common.Constants;
import com.study.dubbo.api.IHello;
import com.study.dubbo.consumer.service.TestService;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.stereotype.Service;

/**
 * @author yuantongqin
 * 2022/2/25
 */
@Service
public class TestServiceImpl implements TestService {


    @DubboReference(
            version = "1.0.0",
            timeout = 50000,
            loadbalance = "random",
            mock = "com.study.dubbo.consumer.service.impl.MockHelloService",
            cluster="failfast"
    )
    private IHello demoService;

    @Override
    public String test(String t) {
        System.out.println("调用服务端");
        return demoService.sayHello(t);
    }

    /**
     * 服务泛化接口调用
     * @return
     */
    public String test1(){
        // 引用远程服务
// 该实例很重量，里面封装了所有与注册中心及服务提供方连接，请缓存
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
// 弱类型接口名
        reference.setInterface("com.xxx.XxxService");
        reference.setVersion("1.0.0");
// 声明为泛化接口
        reference.setGeneric("true");
        reference.setGeneric(true);

// 用org.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
        GenericService genericService = reference.get();

// 基本类型以及Date,List,Map等不需要转换，直接调用
        Object result = genericService.$invoke("sayHello", new String[] {"java.lang.String"}, new Object[] {"world"});
        return null;
    }

}
